﻿Partial Class Administracion_Operadores
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            'cargar operador
            cargarOperadores()
        End If
    End Sub

    Sub cargarOperadores()
        Dim bd As New BdHogaresDataContext()
        Dim a = From u In bd.web_Operadores
                Select u.Nombre, u.Municipio.Departamento.Departamento, u.Municipio.Municipio, u.idOperador
        grdoperadores.DataSource = a
        grdoperadores.DataBind()
    End Sub

    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        pnlRegistrarOperador.Visible = True
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        pnlRegistrarOperador.Visible = False
    End Sub

    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If Page.IsValid = False Then
            Exit Sub
        End If
        If String.IsNullOrEmpty(hdIdoperador.Value) Or hdIdoperador.Value = 0 Then
            insertarOperador()
        Else
            ActualizaOperador(hdIdoperador.Value)
        End If
    End Sub
    Sub insertarOperador()
        Dim u As New web_Operadore
        u.Nombre = txtNombreoperador.Text
        u.CodMunicipio = Municipio.CodMunicipio
        Using bd As New BdHogaresDataContext
            bd.web_Operadores.InsertOnSubmit(u)
            bd.SubmitChanges()
            bd.registrarLog(Context.User.Identity.Name, 1, Request.CurrentExecutionFilePath)
        End Using
        Dim script = "window.location ='" & Context.Request.Url.ToString & "';"
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Abrir", script, True)
    End Sub
    Sub ActualizaOperador(ByVal idoperador As Integer)
        Using bd As New BdHogaresDataContext
            Dim a = From us In bd.web_Operadores
                            Where us.idOperador = CInt(idoperador)
                            Select us
            Dim u As New web_Operadore
            u = a.FirstOrDefault
         u.Nombre = txtNombreoperador.Text
            u.CodMunicipio = Municipio.CodMunicipio
            bd.SubmitChanges()
            bd.registrarLog(Context.User.Identity.Name, 1, Request.CurrentExecutionFilePath)
        End Using
        Dim script = "window.location ='" & Context.Request.Url.ToString & "';"
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Abrir", script, True)
    End Sub





    Protected Sub grdoperadores_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdoperadores.RowCommand
        Select Case e.CommandName
            Case "Editar"
                Using bd As New BdHogaresDataContext
                    Dim a = From u In bd.web_Operadores
                            Where u.idOperador = CInt(e.CommandArgument)
                            Select u

                    Dim us As New web_Operadore()
                    us = a.FirstOrDefault
                    hdIdoperador.Value = us.idOperador
                    txtNombreoperador.Text = us.Nombre
                    Municipio.cargarMunicipio(us.CodMunicipio)
                   
                    pnlRegistrarOperador.Visible = True


                End Using


            Case "Eliminar"
                Try
                    Using bda As New bdSeguridadDataContext
                        Dim a = From u In bda.Web_Usuarios
                                Where u.IdOperador = CInt(e.CommandArgument)
                                Select u

                        Dim us As New Web_Usuario()
                        us = a.FirstOrDefault
                        hdIdoperadorexiste.Value = us.IdOperador
                        If hdIdoperadorexiste.Value <> 0 Then
                            Response.Write("<script>alert('No se puede eliminar el operador por que tiene usuarios asociados a el, elimine estos antes')</script>")
                            Exit Sub
                        End If

                    End Using
                Catch ex As Exception
                End Try
                Using bd As New BdHogaresDataContext
                    Dim a As Integer = e.CommandArgument
                    bd.usp_eliminaroperadorbyid(a)
                    bd.SubmitChanges()
                End Using
                cargarOperadores()
            Case Else
                Exit Sub
        End Select
    End Sub


End Class

